package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SalaryBillSendAddDTO;
import org.jsola.hr.dto.SalaryBillSendUpdateDTO;
import org.jsola.hr.query.SalaryBillSendQuery;
import org.jsola.hr.query.SalaryBillSendRelQuery;
import org.jsola.hr.service.ISalaryBillSendService;
import org.jsola.hr.vo.SalaryBillListVO;
import org.jsola.hr.vo.SalaryBillSendListVO;
import org.jsola.hr.vo.SalaryBillSendVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 工资条发放信息
 *
 * @author zhr
 */
@RestController("hrSalaryBillSendController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryBillSends")
public class SalaryBillSendController implements ISalaryBillSendControllerAPI {

    @Autowired
    private ISalaryBillSendService salaryBillSendService;

    @Override
    @PostMapping
    public Result<SalaryBillSendVO> save(@Valid @RequestBody SalaryBillSendAddDTO salaryBillSendAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增工资条发放信息权限校验

        return Result.success(
                salaryBillSendService.save(salaryBillSendAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryBillSendUpdateDTO salaryBillSendUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资条发放信息权限校验

        int count = salaryBillSendService.update(salaryBillSendUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryBillSendId}")
    public Result<Integer> delete(@PathVariable String salaryBillSendId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除工资条发放信息权限校验

        int count = salaryBillSendService.deleteByIds(tokenUser, salaryBillSendId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryBillSendIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除工资条发放信息权限校验

        int count = salaryBillSendService.deleteByIds(tokenUser, salaryBillSendIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryBillSendId}")
    public Result<SalaryBillSendVO> findById(@PathVariable String salaryBillSendId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
            salaryBillSendService.selectById(salaryBillSendId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryBillSendListVO>> listAll(@Valid SalaryBillSendQuery salaryBillSendQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
            salaryBillSendService.select(salaryBillSendQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryBillSendListVO>> page(@Valid SalaryBillSendQuery salaryBillSendQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryBillSendService.selectPage(salaryBillSendQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/salaryBillSendDetailTab")
    public Result<SalaryBillSendVO> salaryBillSendDetailTab(@Valid SalaryBillSendQuery salaryBillSendQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
                salaryBillSendService.salaryBillSendDetailTab(salaryBillSendQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/salaryBillSendListPage")
    public Result<Page<SalaryBillSendListVO>> salaryBillSendListPage(@Valid SalaryBillSendQuery salaryBillSendQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
                salaryBillSendService.salaryBillSendListPage(salaryBillSendQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/salaryBillListPageBySend")
    public Result<Page<SalaryBillListVO>> salaryBillListPageBySend(@Valid SalaryBillSendRelQuery salaryBillSendRelQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
                salaryBillSendService.salaryBillListPageBySend(salaryBillSendRelQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/salaryBillPageBySendDetail")
    public Result<Page<SalaryBillListVO>> salaryBillPageBySendDetail(@Valid SalaryBillSendRelQuery salaryBillSendRelQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看工资条发放信息权限校验

        return Result.success(
                salaryBillSendService.salaryBillPageBySendDetail(salaryBillSendRelQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "withdraw")
    public Result<Integer> withdraw(@Valid @RequestBody SalaryBillSendUpdateDTO salaryBillSendUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改工资条发放信息权限校验

        int count = salaryBillSendService.withdraw(salaryBillSendUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("操作失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

